package alejandro.castro.asteroid;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Semaphore;

import alejandro.castro.asteroid.components.Asteroid;
import alejandro.castro.asteroid.components.Ship;
import alejandro.castro.asteroid.components.Shot;
import alejandro.castro.controllers.CollisionController;
import alejandro.castro.extras.AsteroidInstantiator;
import alejandro.castro.extras.ComponentsRemover;
import alejandro.castro.extras.MusicController;

import com.uqbar.vainilla.GameScene;

public class AsteroidScene extends GameScene {

	private List<Asteroid> asteroids;
	private List<Shot> shots;
	
	public Semaphore componentsAdding;
	

	public AsteroidScene() {
		super();
		this.componentsAdding = new Semaphore(1);
		this.asteroids = new LinkedList<Asteroid>();
		this.shots = new LinkedList<Shot>();
		this.addComponent(new Ship(300, 300));
		this.addComponent(new MusicController());
	}
	
	public void addAsteroid(Asteroid asteroid){
		this.asteroids.add(asteroid);
		this.addComponent(asteroid);
	}
	
	public void removeAsteroid(Asteroid asteroid){
		this.asteroids.remove(asteroid);
		asteroid.setDestroyPending(true);
	}
	
	public void removeAsteroids(List<Asteroid> removedAsteroids){
		for (Asteroid removedAsteroid : removedAsteroids) {			
			this.removeAsteroid(removedAsteroid);
		}
	}
	
	public void addShot(Shot shot){
		this.shots.add(shot);
		this.addComponent(shot);
	}
	
	public void removeShot(Shot shot){
		this.shots.remove(shot);
		shot.setDestroyPending(true);
	}
	
	public void removeShots(List<Shot> removedShots){
		for (Shot shot : removedShots) {			
			this.removeShot(shot);
		}
	}

	public List<Asteroid> getAsteroids() {
		return asteroids;
	}

	public List<Shot> getShots() {
		return shots;
	}
	
	@Override
	public void onSetAsCurrent() {
		super.onSetAsCurrent();		
		//Threads
		new AsteroidInstantiator(this).start();
		new ComponentsRemover(this).start();		
		//controllesrs
		addComponent(new CollisionController());
	}

	
	
}
